﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using DistributeTransaction.Models;

namespace DistributeTransaction.DAL
{
    public class PaymentRepository : GenericRepository<Payment> 
    {
        public PaymentRepository(DatabaseContext context)
            : base(context)
        {

        }

        public IEnumerable<Payment> GetTempPaymentForShoppingCart(Guid userId)
        {
            const string sql = @"SELECT P.[PaymentId]
	  , 0 AS OrderId
      ,P.[AccountId]
      ,P.[Amount]
      ,P.[Status]
      ,P.[ProcessDate]
	  ,C.CardNumber
	  ,C.HoldName
	  ,C.CardType
FROM [dbo].[PaymentTmp] P
JOIN dbo.CardInfo C ON P.AccountId = C.AccountId
WHERE P.UserId = @p0";
            return GetWithRawSql(sql, userId).ToList();
        }

        internal bool SaveCurrentPayment(Payment payment, Guid userId)
        {
            const string sql = @"dbo.Insert_Payment";

            SqlParameter pUserId = new SqlParameter();
            pUserId.ParameterName = "UserId";
            pUserId.Direction = ParameterDirection.Input;
            pUserId.DbType = DbType.Guid;
            pUserId.Value = userId;

            SqlParameter pAccountId = new SqlParameter();
            pAccountId.ParameterName = "AccountId";
            pAccountId.Direction = ParameterDirection.Input;
            pAccountId.DbType = DbType.Int32;
            pAccountId.Value = payment.AccountId;

            SqlParameter pAmount = new SqlParameter();
            pAmount.ParameterName = "Amount";
            pAmount.Direction = ParameterDirection.Input;
            pAmount.DbType = DbType.Currency;
            pAmount.Value = payment.Amount;

            SqlParameter pPaymentId = new SqlParameter();
            pPaymentId.ParameterName = "PaymentId";
            pPaymentId.Direction = ParameterDirection.Output;
            pPaymentId.DbType = DbType.Int32;

            return ExecuteStoreProcedure(sql, pUserId, pAccountId, pAmount, pPaymentId);
        }

        internal bool DeletePayment(int paymentId, Guid userId)
        {
            const string sql = @"dbo.Delete_Payment";

            SqlParameter pPaymentId = new SqlParameter();
            pPaymentId.ParameterName = "PaymentId";
            pPaymentId.Direction = ParameterDirection.Input;
            pPaymentId.DbType = DbType.Int32;
            pPaymentId.Value = paymentId;

            return ExecuteStoreProcedure(sql, pPaymentId);
        }

        internal IEnumerable<Payment> GetOrderPayments(int orderId)
        {
            const string sql = @"SELECT P.[PaymentId]
      ,P.OrderId
      ,P.[AccountId]
      ,P.[Amount]
      ,P.[Status]
      ,P.[ProcessDate]
	  ,C.CardNumber
	  ,C.HoldName
	  ,C.CardType
FROM [dbo].[Payment] P
JOIN dbo.CardInfo C ON P.AccountId = C.AccountId
WHERE P.OrderId = @p0";
            return GetWithRawSql(sql, orderId).ToList();
        }
    }
}